<?php
namespace app\api\controller;

use app\api\controller\Base;

/**
 * 公共信息
 */
class Coupon extends Base
{
    /**
     * 获取用户当前可用优惠券列表
     *
     */
    public function usercouponlist()
    {
        if (request()->isPost()) {
            $user = $this->user_id;
            $list = db('coupon_user')
                ->where(['status'=>0,'user_id'=>$user])
                ->select();
            foreach ($list as $i => $item) {
               $list[$i]['coupon_info'] = unserialize($item['coupon_info']);
            }            
            return model('Res')->ctx($list);
        } else {
            return model('Res')->noPost();
        }
    }
    public function usercouponinfo()
    {
        if (request()->isPost()) {
            $id = input('post.id');
            $list = db('coupon_user')
                ->where(['id'=>$id])
                ->find();           
            if($list){
                $list['coupon_info'] = unserialize($list['coupon_info']);
            }   
            return model('Res')->ctx($list);
        } else {
            return model('Res')->noPost();
        }
    }
    // 优惠券与活动优惠券入口
    public function getcouponlist()
    {
        if (request()->isPost()) {
            $id = input('post.id'); 
            if(isset($id)){
                // 活动优惠券入口
                $ls = db('activity')
                    ->where(['id'=>$id])
                    ->field('param')
                    ->find()['param'];
                $ls = explode(',',$ls);
                $list = db('coupon')
                    ->where(['is_activity'=>1,'id'=>['in',$ls]])
                    ->select();
                $user_id = $this->user_id;
                foreach ($list as $i => $item) {
                    $ur = db('coupon_user')->where(['user_id'=>$user_id,'coupon_id'=>$item['id']])->find();
                    $list[$i]['cate'] = db('coupon_cate')->where(['id'=>$item['cate_id']])->find()['title'];
                    $list[$i]['start_time'] = date('Y/m/d',$item['start_time']);
                    $endtime = $item['start_time']+$item['end_time']*24*60*60;
                    if($item['start_time']==0){
                        $list[$i]['des'] = $item['end_time'].'天有效期';
                    }
                    if($item['end_time']==0){
                        $list[$i]['des'] = '无期限';
                    }
                    $list[$i]['end_time'] = date('Y/m/d', $endtime);
                    if($ur){
                        $list[$i]['isuse'] = 1;
                    }else{
                        $list[$i]['isuse'] = 0;
                    }
                }
               
                
            }
            return model('Res')->ctx($list);
        } else {
            return model('Res')->noPost();
        }
    }
    // 优惠券详情
    public function getcouponinfo()
    {
        if(request()->isPost()){
            $id = input('post.id');
            if(isset($id)){
                $list = db('coupon')->where(['id'=>$id])->find();

                $list['cate'] = db('coupon_cate')->where(['id'=>$list['cate_id']])->find()['title'];
                
                $list['start_time'] = date('Y/m/d',$list['start_time']);
                    $endtime = $list['start_time']+$list['end_time']*24*60*60;
                    if($list['start_time']==0){
                        $list['des'] = $list['end_time'].'天有效期';
                    }
                    if($list['end_time']==0){
                        $list['des'] = '无期限';
                    }
                    $list['end_time'] = date('Y/m/d', $endtime);
                
                return model('Res')->ctx($list);
            }else{
                return model('Res')->noParam();
            }
        }
    }



}
